package duckHunter.ui;

import java.io.IOException;
import java.io.InputStream;

import javax.microedition.media.Manager;
import javax.microedition.media.MediaException;
import javax.microedition.media.Player;
/**
 * SoundPlayer used to play sounds
 * @author BAZOUN
 *
 */
public class SoundPlayer {
	private InputStream is;
	private Player p;

	//constructor
	//takes a string of the file path in the resources
	public SoundPlayer(String file){
		try {
			//creates the inputStream and the player of wav audio files
			is = this.getClass().getResourceAsStream(file); 
			p = Manager.createPlayer( is, "audio/X-wav" );
		} catch (IOException e) {
			e.printStackTrace();
		} catch (MediaException e) {
			e.printStackTrace();
		}

	}
	
	//play the sound
	public void play() throws IOException, MediaException{
		p.stop();
		p.realize();
		p.prefetch();
		p.start();
	}

	//stop the sound
	public void stop() {
		try {
			p.stop();
		} catch (MediaException e) {
			e.printStackTrace();
		}
	}


}
